#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/14 10:10
# @Author  : 王凯
# @File    : guangxi_illegal.py
# @Project : spider-man

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class GuangxiIllegalSpider(BaseTaxIllegalSpider):
    name: str = "guangxi_illegal"
    province: str = "广西"
    url: str = "http://guangxi.chinatax.gov.cn/nsfw/sscx/sswfanj/"  # 取首页

    def start_requests(self):
        url = "https://guangxi.chinatax.gov.cn/restSearch?channelid=290909&searchword=&orderby=RELEVANCE&page=1&pageSize=5000"
        yield self.Request(url)
        yield from self.etax_search(**{"etax_url": "https://etax.guangxi.chinatax.gov.cn:8443"})

    def parse(self, response, **kwargs):
        data_list = response.json().get("datas", [])
        mapping_dict = {
            "NSR_NAME": "company_name",
            "REG_ADDR": "address",
            "NSR_ID": "taxpayer_id",
            "ZZJG_CODE": "org_code",
            "FDDBR_NAME:FDDBR_SEX:FDDBR_SFZHM": "legal_representative",
            "CWFZR_NAME:CWFZR_SEX:CWFZR_SFZHM": "resp_financial",
            "SJFZR_NAME:SJFZR_SEX:SJFZR_SFZHM": "resp_person",
            "ZJZR_ZJJGXX": "resp_intermediary",
            "AJXZ": "illegal_status",
            "ZYWFSS": "illegal_facts",
            "XGFL_CFQK": "basis_and_punishment",
        }
        for data in data_list:
            item = NetTaxIllegalProItem()
            item.province = self.province
            item.year = data["DOCPUBTIME"][:10].replace(".", "")
            for i in mapping_dict.keys():
                if ":" in i:
                    value_list = []
                    for i_name in i.split(":"):
                        if data.get(i_name):
                            value_list.append(data.get(i_name))
                    setattr(item, mapping_dict.get(i), " ".join(value_list) if value_list else None)
                else:
                    setattr(item, mapping_dict.get(i), None if data.get(i) == "--" else data.get(i))
            yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "guangxi_illegal"])
